unit CHILDWIN;

interface

uses Windows, Classes, Graphics, Forms, Controls, StdCtrls,
  SysUtils, Variants, ShellApi, Contnrs,
  cxClasses, dxBar,
  cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxStyles,
  cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxGridLevel, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxGridExportLink, ADODB, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinFoggy, dxSkinGlassOceans, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinPumpkin, dxSkinSeven,
  dxSkinSharp, dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinXmas2008Blue,
  dxSkinscxPCPainter, dxSkinsdxBarPainter;

type
  TMDIChild = class(TForm)
    dxBarManager1: TdxBarManager;
    dxBarManager1Bar1: TdxBar;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    dxmExcel: TdxBarButton;
    Query: TADOQuery;
    DataSource1: TDataSource;
    dxmRefresh: TdxBarButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure dxmExcelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure dxmRefreshClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    queries: TObjectList;
  public
    { Public declarations }
    ID: Integer;
    procedure Init;
  end;

implementation

{$R *.dfm}

uses Main;

procedure TMDIChild.dxmExcelClick(Sender: TObject);
var
  s: String;
begin
  s := ExtractFilePath(ParamStr(0))+'Table.xls';
  ExportGridToExcel(s, cxGrid1);
  ShellExecute(Handle, 'open', PWideChar(s), nil, nil, SW_SHOWNORMAL)
end;

procedure TMDIChild.dxmRefreshClick(Sender: TObject);
var
  i: Integer;
begin
  Query.Close;
  for i := 0 to queries.Count - 1 do begin
    TADOQuery(queries[i]).Close;
    TADOQuery(queries[i]).Open;
  end;
  Query.Open;
end;

procedure TMDIChild.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TMDIChild.FormCreate(Sender: TObject);
begin
  queries := TObjectList.Create;
  queries.OwnsObjects := True;
end;

procedure TMDIChild.FormDestroy(Sender: TObject);
begin
  queries.Free;
end;

procedure TMDIChild.Init;
var
  i: Integer;
  col: TcxGridDBColumn;
  fld: TStringField;
  s, sTbl, sKey, sRes: String;
  qry: TADOQuery;
begin
  with MainForm do begin
    tTables.Locate('ID', ID, []);
    Self.Caption := tTablesTableDescription.AsString;
    Query.SQL.Text := 'Select * From '+tTablesTableName.AsString;

    Query.FieldDefs.Update;
     // create all defaults fields
     for i := 0 to Query.FieldDefList.Count - 1 do
       with Query.FieldDefList[I] do
         if (DataType <> ftUnknown) and not (DataType in ObjectFieldTypes) and
           not ((faHiddenCol in Attributes) and not Query.FIeldDefs.HiddenFields) then
           CreateField(Self, nil, Query.FieldDefList.Strings[I]);
    tTablesFields.First;
    tTablesFields.Locate('TableID', ID, []);
    while (tTablesFieldsTableID.AsInteger = ID) and not tTablesFields.Eof do begin
      s := tTablesFieldsLookUp.AsString;
      if s <> '' then begin
        i := Pos(';', s);
        sTbl := Copy(s, 1, i-1);
        s := Copy(s, i+1, MaxInt);
        i := Pos(';', s);
        sKey := Copy(s, 1, i-1);
        sRes := Copy(s, i+1, MaxInt);
        fld := TStringField.Create(Query);
        fld.FieldName := tTablesFieldsFieldName.AsString+'_LU';
        fld.FieldKind := fkLookup;
        fld.DataSet := Query;
        fld.Name := Query.Name + fld.FieldName;
        fld.KeyFields := tTablesFieldsFieldName.AsString;
        qry := TADOQuery.Create(Self);
        qry.Name := tTablesFieldsFieldName.AsString+'_LUQ';
        qry.Connection := MainForm.ADOConnection1;
        qry.SQL.Text := 'Select * From '+sTbl;
        qry.Open;
        fld.LookupDataSet := qry;
        fld.LookupKeyFields := sKey;
        fld.LookupResultField := sRes;
        queries.Add(qry);

        Query.FieldDefs.Add(fld.FieldName, ftString, tTablesFieldsWidth.AsInteger, False);
      end;

      tTablesFields.Next;
    end;
    Query.Open;
    cxGrid1DBTableView1.DataController.CreateAllItems;
    tTablesFields.First;
    tTablesFields.Locate('TableID', ID, []);
    while (tTablesFieldsTableID.AsInteger = ID) and not tTablesFields.Eof do begin
      col := cxGrid1DBTableView1.GetColumnByFieldName(tTablesFieldsFieldName.AsString);
      if tTablesFieldsLookUp.AsString <> '' then begin
        col.Visible := False;
        col := cxGrid1DBTableView1.GetColumnByFieldName(tTablesFieldsFieldName.AsString+'_LU');
      end;
      col.Caption := tTablesFieldsFieldCaption.AsString;
      if tTablesFieldsWidth.AsInteger = 0 then
        col.Visible := False
      else
        col.Width := tTablesFieldsWidth.AsInteger;
      col.Index := tTablesFieldsNN.AsInteger;

      tTablesFields.Next;
    end;
  end;
end;

end.
